package com.lmm.lcbusiness.lcbusiness.pages.chooseplace;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.lmm.lcbusiness.lcbusiness.AppBaseActivity;
import com.lmm.lcbusiness.lcbusiness.R;
import com.lmm.lcbusiness.lcbusiness.utils.BDGPSUtils;
import com.lmm.lcbusiness.lcbusiness.utils.GPSReturn;


/**
 * 选择位置
 *
 * @author xmgong
 */
@Route(path = "/app/chooseplace")
public class ChoosePointActivity extends AppBaseActivity implements View.OnClickListener, GPSReturn, BaiduMap.OnMapStatusChangeListener {

    MapView mMapView = null;
    ImageView ivLocation;//定位按钮
    BaiduMap mBaiduMap;


    double lat = 0;
    double lon = 0;

    BDGPSUtils mbdgpsutil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_place);
        setActionBarTitle("选择位置");
        initView();
        initData();
    }

    private void initData() {
        if (getIntent().hasExtra("jingdu")) {
            lon = (getIntent().getDoubleExtra("jingdu", 0));
        }
        if (getIntent().hasExtra("weidu")) {
            lat = (getIntent().getDoubleExtra("weidu", 0));
        }

        if (getIntent().hasExtra("lat")) {
            lat = getIntent().getDoubleExtra("lat", 0);
        }
        if (getIntent().hasExtra("lon")) {
            lon = getIntent().getDoubleExtra("lon", 0);
        }
        mbdgpsutil = new BDGPSUtils(this, this);
        if (lon == 0 || lat == 0) {
            mbdgpsutil.startGPS();
        } else {
            LatLng ll = new LatLng(lat, lon);
            mMapStatus = new MapStatus.Builder()
                    .target(ll)
                    .build();
            MarkPoint(ll);
            //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
            MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
            //改变地图状态
            mBaiduMap.animateMapStatus(mMapStatusUpdate);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_sure, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        if (item.getItemId() == R.id.action_sure) {
            Intent mintent = new Intent();
            mintent.putExtra("lat", lat);
            mintent.putExtra("lon", lon);
            mintent.putExtra("address", "");
            setResult(RESULT_OK, mintent);
            finish();
        }
        return true;
    }

    private void initView() {
        mMapView = findViewById(R.id.map);
        ivLocation = findViewById(R.id.iv_locaion);
        ivLocation.setOnClickListener(this);
        //设置为普通地图
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMyLocationEnabled(true);
        mBaiduMap.setOnMapStatusChangeListener(this);
    }

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        mbdgpsutil.stopGPS();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }


    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }


    @Override
    public void onClick(View v) {
        if (v == ivLocation) {
            mbdgpsutil.startGPS();
        }
    }


    /**
     * 标注位置
     */
    MapStatus mMapStatus = null;

    @Override
    public void retrunGPSInfo(BDLocation location) {
        if (location != null) {
            LatLng ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            mbdgpsutil.stopGPS();
            MarkPoint(ll);
            mMapStatus = new MapStatus.Builder()
                    .target(ll)
                    .build();
            //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
            MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
            //改变地图状态
            mBaiduMap.animateMapStatus(mMapStatusUpdate);
        }
    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus, int i) {

    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {
    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mMapStatus) {
        lat = mMapStatus.target.latitude;
        lon = mMapStatus.target.longitude;
        MarkPoint(mMapStatus.target);
    }

    /**
     * @param point Maker坐标点
     */
    private void MarkPoint(LatLng point) {
        lat = point.latitude;
        lon = point.longitude;
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_gcoding);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        mBaiduMap.clear();
        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
    }
}
